import defaultImg from '@/components/defaultImg/defaultImg'

export default {
  data() {
    return {
      list: [
        {
          name: '全部',
        },
        {
          name: '待付款',
        },
        {
          name: '待备货',
        },
        {
          name: '待到店',
        },
        {
          name: '待施工',
        },
        {
          name: '已完成',
        },
      ],
      loadText: ['loadmore', 'loading', 'nomore'],
      loadStatus: 0,
      // 因为内部的滑动机制限制，请将tabs组件和swiper组件的current用不同变量赋值
      current: 0, // tabs组件的current值，表示当前活动的tab选项
      swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
      pageIndex: 1,
      pageSize: 10,
      status: -1, //订单状态  // 状态 -1：全部，0：待付款，1：待备货，,2：待到店，3：待施工，4：已完成
      // tabs: [1, 2, 3, 4,5,6]
      orderList: [],
      showLoad: false,
      totalPage: 0,
      statusList: [
        {
          id: 0,
          name: '待确认',
        },
        {
          id: 5,
          name: '待付款',
        },
        {
          id: 10,
          name: '已取消',
        },
        {
          id: 20,
          name: '待备货',
        },
        {
          id: 30,
          name: '待到店',
        },
        {
          id: 40,
          name: '待施工',
        },
        {
          id: 50,
          name: '施工中',
        },
        {
          id: 60,
          name: '已完成',
        },
      ],
    }
  },
  onShow() {
    this.pageIndex = 1
    this.orderList = []
    this.GetBusOrderList()
  },
  onLoad() {
    this.GetBusOrderList()
  },
  components: {
    defaultImg,
  },
  methods: {
    // 获取列表信息
    GetBusOrderList() {
      let data = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        status: this.status,
      }
      uni.showLoading({
        title: '加载中',
      })
      this.$api.afterSales
        .GetBusOrderList(data)
        .then((res) => {
          // console.log(res)
          uni.hideLoading()
          this.showLoad = true
          this.orderList = this.orderList.concat(res.data.list)
          this.totalPage = res.data.total
          // 判断显示哪种分页状态
          if (Math.ceil(this.totalPage / 10) > this.pageIndex) {
            this.loadStatus = 0
          } else {
            this.loadStatus = 2
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    returnStatus(id) {
      let status = ''
      this.statusList.forEach((it) => {
        if (it.id === id) {
          status = it.name
        }
      })
      return status
    },
    returnTime(time) {
      if (time) {
        return time.split('T')[0] + ' ' + time.split('T')[1]
      }
    },
    // 跳转详情
    goDetail(it) {
      uni.navigateTo({
        url: '/pages_afterSales/order/orderDetail/orderDetail?id=' + it.id,
      })
    },

    // tabs通知swiper切换
    tabsChange(index) {
      this.swiperCurrent = index
      this.showLoad = false
      this.status = this.swiperCurrent - 1
      this.pageIndex = 1
      this.orderList = []
      this.GetBusOrderList()
    },
    // swiper-item左右移动，通知tabs的滑块跟随移动
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    // 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
    // swiper滑动结束，分别设置tabs和swiper的状态
    animationfinish(e) {
      let current = e.detail.current
      this.$refs.uTabs.setFinishCurrent(current)
      // console.log(this.swiperCurrent,current)
      if (this.swiperCurrent === current) {
      } else {
        // console.log(1122)
        this.showLoad = false
        this.swiperCurrent = current
        this.current = current
        this.status = this.swiperCurrent - 1
        this.pageIndex = 1
        this.orderList = []
        this.GetBusOrderList()
      }
    },
    // scroll-view到底部加载更多
    onreachBottom() {
      if (Math.ceil(this.totalPage / 10) > this.pageIndex) {
        this.loadStatus = 1
        this.pageIndex++
        this.GetBusOrderList()
        // console.log(1111)
      } else {
        this.loadStatus = 2
        // console.log(222)
      }
    },
  },
}
